/**
 * 
 */
package com.mobile2you.nci.android;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mobile2you.nci.android.data.WebServicesRequest;
import com.mobile2you.nci.android.domain.Annuaires;
import com.mobile2you.nci.android.utils.Utils;

/**
 * @author JBromo
 * 
 */
public class MenuAnnuaire extends Activity {

	private String login, password;

	private ProgressDialog sendDialog;

	private class LoadAnnuaireTask extends
			AsyncTask<String, String, List<Annuaires>> {

		@Override
		protected List<Annuaires> doInBackground(String... params) {
			return WebServicesRequest.getListAnnuaires(MenuAnnuaire.this,
					login, password);
		}

		@Override
		protected void onPostExecute(List<Annuaires> result) {
			super.onPostExecute(result);
			sendDialog.dismiss();
			if (result != null && !result.isEmpty()) {
				Utils.lancerActiviter(MenuAnnuaire.this,
						MenuAnnuaireDetails.class);
			} else {
				Toast.makeText(MenuAnnuaire.this, "Erreur d'authentification",
						Toast.LENGTH_LONG).show();
			}

		}

	}

	private void initView() {

		final EditText nameEdit = (EditText) findViewById(R.id.edit_nom);
		final EditText phoneEdit = (EditText) findViewById(R.id.edit_phone);
		Button sendButton = (Button) findViewById(R.id.bt_contact_envoyer);
		final Animation animation = AnimationUtils.loadAnimation(this,
				R.anim.editextanim);
		sendButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				login = nameEdit.getText().toString();
				password = phoneEdit.getText().toString();
				if (login == null || login.trim().length() < 1) {
					nameEdit.startAnimation(animation);
					Toast.makeText(MenuAnnuaire.this, "Invalide login",
							Toast.LENGTH_LONG).show();
				} else {
					if (password == null || password.trim().length() < 1) {
						phoneEdit.startAnimation(animation);
						Toast.makeText(MenuAnnuaire.this, "Invalide password",
								Toast.LENGTH_LONG).show();
					} else {
						sendDialog = ProgressDialog.show(v.getContext(), "",
								"Autentification en cours");
						new LoadAnnuaireTask().execute();
					}
				}

			}
		});

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.menu_annuaire);
		initView();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

}
